package com.m3839.sdk.common.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import com.m3839.sdk.common.util.ScreenUtils;

/**
 * Description:MaxHeightView  当前定制为UI设定的最大高度
 *
 * @author 陈宝阳
 * @create 2020/8/24 10:14
 */
public class MaxHeightView extends FrameLayout {

  private static final float DEFAULT_MAX_RATIO = 0.6f;
  private static final float DEFAULT_MAX_HEIGHT = 0f;

  /** 优先级高 */
  private float mMaxRatio = DEFAULT_MAX_RATIO;

  /** 优先级低 */
  private float mMaxHeight = DEFAULT_MAX_HEIGHT;

  public MaxHeightView(Context context) {
    super(context);
    init(context);
  }

  public MaxHeightView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
  }

  public MaxHeightView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
  }

  private void init(Context context){
    mMaxHeight = ScreenUtils.dip2px(context, 340);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    if (heightMode == MeasureSpec.EXACTLY) {
      heightSize = heightSize <= mMaxHeight ? heightSize
          : (int) mMaxHeight;
    }

    if (heightMode == MeasureSpec.UNSPECIFIED) {
      heightSize = heightSize <= mMaxHeight ? heightSize
          : (int) mMaxHeight;
    }
    if (heightMode == MeasureSpec.AT_MOST) {
      heightSize = heightSize <= mMaxHeight ? heightSize
          : (int) mMaxHeight;
    }
    int maxHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize,
        heightMode);
    super.onMeasure(widthMeasureSpec, maxHeightMeasureSpec);
  }



}
